/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Processor.Components;

import CrlException.CrlException;
import Data.Frontier.URLCrl;
import Data.ProcessData.ProcessData;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import ExtractorInterface.ExtractorInterface;

/**
 *
 * @author CUHUUHOANG
 */
public class ExtractURL {

    ProcessData pData;
    ExtractorInterface extractor;

    public ExtractURL(ProcessData pData, ExtractorInterface extractor) {
        this.pData = pData;
        this.extractor = extractor;
    }
    
    public void doExtractURL() throws CrlException {
        try {
            //get array url from page
            Document doc = pData.getDoc();
            Elements links = doc.select("a[href]");
            int length = links.size();

            String[] allURLinPage = new String[length];

            for (int t = 0; t < links.size(); t++) {
                allURLinPage[t] = links.get(t).absUrl("href");

            }
            //use extractor to filter url
            String[] urlAfterFilter = extractor.filterURL(allURLinPage);
            //add url to frontier
            for (int i = 0; i < urlAfterFilter.length; i++) {
                URLCrl.urlDT.addURL(urlAfterFilter[i], pData.getDepth() + 1);
            }
        } catch (Exception e) {
            throw new CrlException(e.getMessage());
        }

    }
}
